其他
Spring Boot系列之JDBC操作数据库
PS:任何时候都不必害怕改变,因为那正是让你变好的路。
安装MySQL
连接MySQL
创建数据库
依赖及配置
实体类
实现增删改查
测试效果
多数据源配置
安装MySQL
连接MySQL
创建数据库和表
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`name` varchar(32) DEFAULT NULL COMMENT '用户名',
`password` varchar(32) DEFAULT NULL COMMENT '密码',
`age` int DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
依赖及配置
// jdbc依赖
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
// mysql驱动
runtime("mysql:mysql-connector-java")
// ...
}
spring.datasource.username=root
# 数据库密码
spring.datasource.password=admin
# JDBC Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JDBC URL
spring.datasource.url=jdbc:mysql://localhost:3306/db_student?serverTimezone=Asia/Shanghai
实体类
student
对应的数据实体类如下:* 实体类
*/
public class Student {
private long id;
private String name;
private String password;
private int age;
public Student() {
}
public Student(String name, String password, int age) {
this.name = name;
this.password = password;
this.age = age;
}
public Student(long id,String name, String password, int age) {
this.id = id;
this.name = name;
this.password = password;
this.age = age;
}
// setter、getter方法
}
实现增删改查
IStudentRepository
如下:* @Desc: 定义增删查改接口
* @Author: jzman
*/
public interface IStudentRepository {
/**
* 保存数据
* @param student 单条student记录
*/
void save(Student student);
/**
* 删除数据
* @param id 学生id
*/
void delete(long id);
/**
* 更新数据
* @param student 单条student记录
*/
void update(Student student);
/**
* 查询数据
* @param name 姓名
* @return 返回单条记录
*/
Student findByName(String name);
/**
* 查询全部数据
* @return 返回全部记录
*/
List<Student> findAll();
}
StudentRepositoryImpl
实现 IStudentRepository
接口实现增删改查:* @Desc: 具体实现
* @Author: jzman
*/
@Repository
public class StudentRepositoryImpl implements IStudentRepository {
public JdbcTemplate mJdbcTemplate;
/**
* 构造方法自动装配
* @param jdbcTemplate JdbcTemplate
*/
public StudentRepositoryImpl(JdbcTemplate jdbcTemplate) {
this.mJdbcTemplate = jdbcTemplate;
}
@Override
public void save(Student student) {
mJdbcTemplate.update("INSERT INTO student(name,password,age) values(?,?,?) ",
student.getName(), student.getPassword(), student.getAge());
}
@Override
public void delete(long id) {
mJdbcTemplate.update("DELETE FROM student where id=?", id);
}
@Override
public void update(Student student) {
mJdbcTemplate.update("UPDATE student SET name=?,password=?,age=? WHERE id=?",
student.getName(), student.getPassword(), student.getAge(), student.getId());
}
@Override
public Student findByName(String name) {
Object[] args = {name};
return mJdbcTemplate.queryForObject("SELECT * FROM student WHERE name=?", args,
new BeanPropertyRowMapper<Student>(Student.class));
}
@Override
public List<Student> findAll() {
return mJdbcTemplate.query("SELECT * FROM student",new BeanPropertyRowMapper<>(Student.class));
}
}
测试效果
* @Author: jzman
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentRepositoryTests {
@Autowired
private IStudentRepository mStudentRepository;
@Test
public void testSave(){
Student student1 = new Student("躬行之", "111",3);
Student student2 = new Student(2,"jzman", "123",20);
mStudentRepository.save(student1);
mStudentRepository.save(student2);
}
}
testSave
之后,使用 IDEA 提供的 Database 工具,双击表 student 查看数据表内容,如下:多数据源配置
DataSource
和 JdbcTemplate
,定义多数据源如下:* @Desc: 数据源配置
* @Author: jzman
*/
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
spring.datasource.primary.username=root
spring.datasource.primary.password=admin
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/data_source_one?serverTimezone=Asia/Shanghai
# dataSource1
spring.datasource.secondary.username=root
spring.datasource.secondary.password=admin
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/data_source_two?serverTimezone=Asia/Shanghai
JdbcTemplate
即可操作对应数据源的数据,具体可参考文末提示查看对应源码,可在公众号后台回复【Spring Boot】获取源码链接,Android微信交流群如下: